/**
 * Created by gaojian on 15/9/7.
 */
var express = require('express'),
    bodyParser = require('body-parser'),
    pool = require('./pool'),
    router = express.Router(),
    _ = require('lodash');

router.get('/home',function(req,res,next){
    pool.getConnection(function(error,connect){
        var total = 0;
        connect.query('select count(*) from story',function(err1,result){
            total = _.values(result[0])[0];
        });

        connect.query('select * from story limit '+(req.query.cur_page-1)*req.query.page_size+','+req.query.page_size,function(err,rows){
            res.json({
                "code":200,
                "msg":"success",
                "data": {
                    "list": _.each(rows, function (item) {

                        //替换手机号中间四位为****
                        item.user = item.user.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');

                        //默认最多显示73个字符
                        item.content = _.trunc(item.content, {'length': 73});
                           return item;
                        }),
                    "total":total
                }
            });
            connect.release();
        });
    })
});

router.get('/story/:id',function(req,res,next){
    pool.getConnection(function(error,connect){
        connect.query('select * from story where id="'+req.params.id+'"',function(err,rows){
            res.json({
                "code":200,
                "msg":"success",
                "data": _.each(rows,function(item){

                    //替换手机号中间四位为****
                    item.user = item.user.replace(/(\d{3})\d{4}(\d{4})/,'$1****$2');
                    return item;
                })
            });
            connect.release();
        });
    })
});

module.exports = router;